package ConexaoMysql;

import java.awt.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class Conexao {
    String url =  "jdbc:mysql://127.0.0.1:3306/sistemavendas";
    String usuario = "root";
    String senha = "admiravel";
    Connection conn = null;
    
    
    public void conectar(){
        
        try{
            Class.forName("com.mysql.jdbc.Driver"); 
            
            try{
                conn = DriverManager.getConnection(url, usuario, senha);
                JOptionPane.showMessageDialog(null, "Conectado");                
        } catch (SQLException | HeadlessException ex ){
            JOptionPane.showMessageDialog(null, "Erro de conexão: " +ex.getMessage());
        }
    } catch(ClassNotFoundException objErroDriver){
        JOptionPane.showMessageDialog(null, "Erro no Drive");
    }
 }
    
    public void desconectar(){
        try{
            conn.close();
            JOptionPane.showMessageDialog(null, "Conexao Encerrada");
            
        } catch(SQLException ex){
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Erro ao tentar desconectar");
        }
    }
    
    public String comando (String sql){
        
    try{
        PreparedStatement stat = null;
        stat.execute();        
    }catch(SQLException ex){
        return "Erro";
    }
    return "OK";
    }
    
    public static void main(String[] args){
        Conexao conexao = new Conexao();
        conexao.conectar();
    }
}